import java.io.IOException;

import tool.FileLoader;


public class Config {

	public static String APP_FOLDER = "file:///E:/Data/Applications/LongPlayer";
	public static String SELECTOR_PAGE = APP_FOLDER + "/selectorPage.txt";
	public static String MEDIA_MARK = APP_FOLDER + "/mediaMark.txt";
	
	public static void setSelectorPage(int page) {
		FileLoader fileLoader;
		try {
			fileLoader = new FileLoader(SELECTOR_PAGE);
			fileLoader.overwriteContent(Integer.toString(page));
			fileLoader.close();
		} catch (IOException e) {
			LongPlayer.showException(e);
		}
	}
	
	public static int getSelectorPage() {
		String pageChars = null;
		
		FileLoader fileLoader;
		try {
			fileLoader = new FileLoader(SELECTOR_PAGE);
			pageChars = fileLoader.readContent();
			fileLoader.close();
		} catch (IOException e) {
			LongPlayer.showException(e);
		}
		
		if (pageChars == null) {
			return 0;
		} else {
			return Integer.parseInt(pageChars);
		}
	}
	
	public static void setMediaMark(String mediaName, long mark) {
		FileLoader fileLoader;
		try {
			fileLoader = new FileLoader(MEDIA_MARK);
			fileLoader.overwriteContent(
					mediaName + "|" + Long.toString(mark));
			fileLoader.close();
		} catch (IOException e) {
			LongPlayer.showException(e);
		}
	}
	
	public static MediaMark getMediaMark() {
		String raw = null;
		
		FileLoader fileLoader;
		try {
			fileLoader = new FileLoader(MEDIA_MARK);
			raw = fileLoader.readContent();
			fileLoader.close();
		} catch (IOException e) {
			LongPlayer.showException(e);
		}
		
		if (raw == null) {
			return MediaMark.NO_MEDIA_MARK;
		} else {
			int pos = raw.indexOf("|");
			String name = raw.substring(0, pos);
			long mediaPos = Long.parseLong(raw.substring(pos + 1));
			return new MediaMark(name, mediaPos);
		}
	}
	
	public static void deleteMediaMark() {
		try {
			FileLoader fileLoader = new FileLoader(MEDIA_MARK);
			fileLoader.deleteFile();
		} catch (IOException e) {
			LongPlayer.showException(e);
		}
	}
}
